/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 *
 */
package rad.framework.jboss.remoting.transport.jms;

import java.io.ByteArrayOutputStream;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

import rad.framework.jms.JmsSender;
import rad.framework.jms.MessageCallback;

import org.jboss.remoting.AbstractInvoker;
import org.jboss.remoting.ConnectionFailedException;
import org.jboss.remoting.InvocationRequest;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.marshal.Marshaller;
import org.jboss.remoting.marshal.UnMarshaller;
import org.jboss.remoting.transport.ClientInvoker;

/**
 * This invoker supports oneway web services calls over jms.
 * 
 * @author boudyacho
 * 
 */
public class JmsClientInvoker extends AbstractInvoker implements ClientInvoker {

	private Marshaller marshaller;

	private UnMarshaller unMarshaller;

	private JmsSender jms;

	public JmsClientInvoker(InvokerLocator locator, Map configuration) {
		super(locator, configuration);
	}

	public JmsClientInvoker(InvokerLocator locator) {
		super(locator);
	}

	public void connect() throws ConnectionFailedException {
		jms = new JmsSender();
		jms.setDestinationJndiName(getLocator().getPath());
	}

	protected Object lookup(String name) throws ConnectionFailedException {
		try {
			InitialContext ctx = new InitialContext();
			return ctx.lookup(name);
		} catch (Exception e) {
			throw new ConnectionFailedException("Could not find: " + name, e);
		}
	}

	public boolean isConnected() {
		return jms != null;
	}

	public void disconnect() {
		jms = null;
	}

	public Object invoke(InvocationRequest invocation) throws Throwable {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		getMarshaller().write(invocation, os);
		final String text = os.toString();

		// setCredentials(jms, invocation);
		send(new MessageCallback() {

			public Message createMessage(Session session) throws JMSException {
				TextMessage msg = session.createTextMessage();
				msg.setText(text);
				return msg;
			}
		});

		// only oneway is supported
		return null;
	}

	protected void setCredentials(JmsSender jms2, InvocationRequest invocation) {
		Map metadata = invocation.getRequestPayload();
		if (metadata != null) {
			jms.setUser((String) metadata.get("http.basic.username"));
			jms.setCredentials((String) metadata.get("http.basic.password"));
		}
	}

	protected void send(MessageCallback callback) throws Throwable {
		try {
			jms.create();
			jms.send(callback);
		} finally {
			jms.destroy();
		}
	}
	
	public Marshaller getMarshaller() {
		return marshaller;
	}

	public UnMarshaller getUnMarshaller() {
		return unMarshaller;
	}

	public void setMarshaller(Marshaller marshaller) {
		this.marshaller = marshaller;
	}

	public void setUnMarshaller(UnMarshaller unMarshaller) {
		this.unMarshaller = unMarshaller;
	}
}
